package com.ruoyi.web.controller.V2.font.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.font.service.IQuotationService;
import com.ruoyi.system.V2.tool.mapper.BacklistMapper;
import com.ruoyi.system.V2.tool.service.IBacklistService;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.model.domain.Backlist;
import com.ruoyi.system.model.domain.Quotation;
import com.ruoyi.system.sms.service.ISendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 【供货商报价单】Controller
 *
 * @author ruoyi
 * @date 2023-03-28
 */
@RestController
@RequestMapping("/account/quotation")
public class QuotationController extends BaseController {
    @Autowired
    private IQuotationService quotationService;
    @Autowired
    private IBacklistService backlistService;
    @Autowired
    private BacklistMapper backlistMapper;
    @Autowired
    private ISendSms sendSms;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     *
     */
    @GetMapping("/list")
    public TableDataInfo list(Quotation quotation) {
        startPage();
        List<Backlist> list = quotationService.selectQuotationList(quotation);
        return getDataTable(list);
    }

    /**
     * 我的报价
     */
    @GetMapping("/mybprice")
    public AjaxResult mybprice(Quotation quotation) {

        Map<String, Object> result = new HashMap<>();
        List<Quotation> list = quotationService.mybprice(quotation);
        result.put("rlist", list);
        Backlist backlist = backlistService.selectBacklistById(quotation.getBackId());
        result.put("back", backlist);
        return AjaxResult.success(result);
    }

    /**
     *
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(quotationService.selectQuotationById(id));
    }

    /**
     *
     */
    @PostMapping
    public AjaxResult add(@RequestBody Quotation quotation) {
        Backlist backlist = backlistMapper.selectBacklistById(quotation.getBackId());
        if (Objects.isNull(backlist)) {
            throw new GlobalException("未找到此回收编号！");
        }
        int count = quotationService.insertQuotation(quotation);
        //发送报价短信
        //配置字典后才会发短信
        List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType("quotationSuccess");
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(sysDictData)) {
            SysDictData s = sysDictData.get(0);
            if (Objects.nonNull(s) && StringUtils.equals(s.getDictValue(), "on")) {
                try {
//                    sendSms.sendSmsForNotice(backlist.getTel(), "baojia");
                } catch (Exception e) {
                    System.out.println("短信发送失败" + e.getMessage());
                }
            }
        }
        return toAjax(count);
    }

    /**
     *
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Quotation quotation) {
        return toAjax(quotationService.updateQuotation(quotation));
    }

    /**
     *
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(quotationService.deleteQuotationByIds(ids));
    }
}
